package com.springboot3.test.springboot3testdemo4.demos.web.service;

import com.fasterxml.jackson.databind.JsonNode;
import com.networknt.schema.JsonSchema;
import com.networknt.schema.ValidationMessage;
import java.util.Set;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Created with IntelliJ IDEA.
 *
 * @author： liuziyang
 * @date： 2024/8/23-13:47
 * @description：
 * @modifiedBy：
 * @version: 1.0
 */
@Slf4j
@Service
public class JsonSchemaValidationService {
  private JsonSchema jsonSchema;

  @Autowired
  public void setJsonSchema(JsonSchema jsonSchema) {
    this.jsonSchema = jsonSchema;
  }

  public String validateJson(JsonNode jsonNode) {
    Set<ValidationMessage> errors = jsonSchema.validate(jsonNode);
    if (errors.isEmpty()) {
      log.info("event is valid");
    } else {
      log.info("event is invalid");
    }
    return errors.toString();
  }
}
